package com.woniu.controller;

import com.woniu.entity.FlowerComment;
import com.woniu.entity.FlowerOrderItem;
import com.woniu.service.EvaluateService;
import com.woniu.util.R;
import com.woniu.vo.FlowerCommentVoCZT;
import com.woniu.vo.MyPage;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.File;
import java.io.IOException;
import java.util.List;

@RestController
@Slf4j
@RequestMapping("/evaluate")
public class EvaluateController {
    @Resource
    EvaluateService evaluateService;

    @Resource
    private RedisTemplate<String, Object> redisTemplate;

    // 获取用户的评论列表
    //@GetMapping("/getEvaluateList/{accountId}")
    public List<FlowerComment> getEvaluateList(@PathVariable Integer accountId) {
        return evaluateService.findAllFlowerCommentByAccountId(accountId);
    }

    @GetMapping("/getEvaluateList/{accountId}")
    public List<FlowerCommentVoCZT> getEvaluateListByAccountId(@PathVariable Integer accountId) {
        return evaluateService.findAllFlowerCommentVoByAccountId(accountId);
    }

    @PostMapping("/getEvaluateListToPage")
    public List<FlowerCommentVoCZT> getEvaluateListToPage(@RequestBody MyPage myPage) {
        return evaluateService.findAllFlowerCommentVoByAccountIdAndPAge(myPage);
    }

    @PostMapping("/getCommentsByPageAndFlowerId")
    public List<FlowerCommentVoCZT> getCommentsByPageAndFlowerId(@RequestBody MyPage myPage) {
        return evaluateService.findAllFlowerCommentVoByAccountIdAndPageAndFlowerId(myPage);
    }

    @PostMapping("/getCommentsByFlowerId")
    public List<FlowerCommentVoCZT> getCommentsByFlowerId(@RequestBody MyPage myPage) {
        return evaluateService.findAllFlowerCommentVoByAccountIdAndFlowerId(myPage);
    }

    // 添加评论图片
    @PostMapping("/addEvaluateImage")
    public String addEvaluateImage(@RequestParam("file") MultipartFile file) {
        if (file.isEmpty()) {
            // 处理文件为空的情况
            return null;
        }
        // 定义保存图片的目录
        //String uploadDir = "D:\\Java\\nginx-1.21.6\\html\\image\\Test";

        // 获取原始文件名
        String originalFilename = file.getOriginalFilename();

        // 生成新的文件名，以避免文件名冲突
        //String newFilename = UUID.randomUUID().toString() + "_" + originalFilename;

        // 构建完整的文件保存路径
        //String filePath = uploadDir + newFilename;

        try {
            // 将文件保存到指定路径
            file.transferTo(new File("D:\\Program Files\\nginx-1.26.1\\html\\image\\Test", originalFilename));

            // 在这里可以将图片的相关信息（如文件名、路径等）保存到数据库中

        } catch (IOException e) {
            // 处理文件保存过程中的异常
            e.printStackTrace();
        }
        System.out.println("--------------->" + originalFilename);
        return originalFilename;
    }


    // 删除评论
    @DeleteMapping("/deleteComment/{commentId}")
    public R deleteEvaluate(@PathVariable Integer commentId) {
        int i = evaluateService.deleteFlowerCommentByCommentId(commentId);
        if (i > 0) {
            return R.build(200, "删除评论成功", i);
        } else {
            return R.error("删除评论失败");
        }
    }

    // 添加评论
    @PostMapping("/addComment")
    public R addEvaluate(@RequestBody FlowerComment flowerComment) {
        int i = evaluateService.addFlowerComment(flowerComment);
        if (i > 0) {
            return R.build(200, "添加评论成功", i);
        } else {
            return R.error("添加评论失败");
        }
    }

    /*@PostMapping("/changeToOrder")
    public R changeToOrder(@RequestBody FlowerOrderItem flowerOrderItem) {

        boolean o = evaluateService.changeOrder(flowerOrderItem.getOrderId());
        boolean oi = evaluateService.changeOrderItem(flowerOrderItem.getCartId(), flowerOrderItem.getContentId());


        if (o && oi) {
            return R.build(200, "成功", null);
        } else {
            return R.error("失败");
        }


    }*/

    @PostMapping("/changeToOrder")
    public R changeToOrder(@RequestBody FlowerOrderItem flowerOrderItem) {
        // 调用业务逻辑方法
        boolean o = evaluateService.changeOrder(flowerOrderItem.getOrderId());
        boolean oi = evaluateService.changeOrderItem(flowerOrderItem.getCartId(), flowerOrderItem.getContentId());
        if (o && oi) {
            try {
                // 构建缓存键
                String cacheKey = "orderSelect:" + flowerOrderItem.getOrderId();
                // 清除缓存
                redisTemplate.delete(cacheKey);
                // 如果需要，也可以清除其他相关的缓存
                return R.build(200, "订单状态更新成功，相关缓存已清除", null);
            } catch (Exception e) {
                // 处理异常情况
                return R.build(500, "清除缓存失败", e.getMessage());
            }
        } else {
            // 更新失败的处理逻辑
            return R.error("订单状态更新失败");
        }
    }

    @GetMapping("/getOrderToEval/{orderId}")
    public R getOrderToEval(@PathVariable Long orderId) {
        int i = evaluateService.getOrderToEval(orderId);

        return R.build(200, "获取IsComment成功", i);

    }


}
